package com.testword.service.impl;

import com.testword.entity.dto.UserLoginInforDTO;
import com.testword.entity.po.UserInfo;
import com.testword.entity.query.UserInfoQuery;
import com.testword.entity.query.SimplePage;
import com.testword.entity.vo.PaginationResultVO;
import com.testword.entity.vo.UserLoginInfor;
import com.testword.enums.PageSize;
import com.testword.enums.UserStatusEnum;
import com.testword.exception.BusinessException;
import com.testword.mappers.UserInfoMapper;
import com.testword.mappers.UserLoginInfoMapper;
import com.testword.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 学生信息表
Service实现
 */
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

	private static final Logger logger = LoggerFactory.getLogger(UserInfoServiceImpl.class);

	@Resource
	private UserInfoMapper<UserInfo, UserInfoQuery> userInfoMapper;

	@Autowired
	private UserLoginInfoMapper userLoginInfoMapper;
	/**
	 * 根据条件查询列表
	 */
	public List<UserInfo> findListByParam(UserInfoQuery query) {
		return this.userInfoMapper.selectList(query);
	}

	/**
	 * 根据条件查询数量
	 */
	public Integer findCountByParam(UserInfoQuery query) {
		return this.userInfoMapper.selectCount(query);
	}

	/**
	 * 分页查询
	 */
	public PaginationResultVO<UserInfo> findListByPage(UserInfoQuery query) {
		int count = this.findCountByParam(query);
		int pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();

		SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
		query.setSimplePage(page);
		List<UserInfo> list = this.findListByParam(query);
		PaginationResultVO<UserInfo> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	public Integer add(UserInfo bean) {
		return this.userInfoMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	public Integer addBatch(List<UserInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userInfoMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增/修改
	 */
	public Integer addOrUpdateBatch(List<UserInfo> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.userInfoMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 根据StudentId查询
	 */
	public UserInfo getUserInfoByStudentId(String studentId) {
		return this.userInfoMapper.selectByStudentId(studentId);
	}

	/**
	 * 根据StudentId修改
	 */
	public Integer updateUserInfoByStudentId(UserInfo bean, String studentId) {
		return this.userInfoMapper.updateByStudentId(bean,studentId);
	}

	/**
	 * 根据StudentId删除
	 */
	public Integer deleteUserInfoByStudentId(String studentId) {
		return this.userInfoMapper.deleteByStudentId(studentId);
	}

	/**
	 * 根据NickName查询
	 */
	public UserInfo getUserInfoByNickName(String nickName) {
		return this.userInfoMapper.selectByNickName(nickName);
	}

	/**
	 * 根据NickName修改
	 */
	public Integer updateUserInfoByNickName(UserInfo bean, String nickName) {
		return this.userInfoMapper.updateByNickName(bean,nickName);
	}

	/**
	 * 根据NickName删除
	 */
	public Integer deleteUserInfoByNickName(String nickName) {
		return this.userInfoMapper.deleteByNickName(nickName);
	}


	@Override
	public UserInfo login(String studentId, String nickName,Integer cet4Score,Integer cet6Score) {
		UserInfo userInfo = this.userInfoMapper.selectByStudentId(studentId);
		if(userInfo == null || !userInfo.getNickName().equals(nickName)){
			throw new BusinessException("学号与姓名不匹配");
		}
		if(UserStatusEnum.DISABLE.getStatus().equals(userInfo.getStatus())){
			throw new BusinessException("当前账号已禁用");
		}
		UserInfo updateInfo = new UserInfo();
		updateInfo.setLastLoginTime(new Date());
		updateInfo.setCet4Score(cet4Score);
		updateInfo.setCet6Score(cet6Score);
		this.userInfoMapper.updateByStudentId(updateInfo,userInfo.getStudentId());

		UserInfo resultInfo = this.userInfoMapper.selectByStudentId(studentId);

		return resultInfo;
	}

	@Override
	public List<UserInfoQuery> rank() {
		UserInfoQuery userInfoQuery = new UserInfoQuery();
		userInfoQuery.setOrderBy("cet6_score desc");
		List<UserInfoQuery> userInfoQueryList = this.userInfoMapper.selectList(userInfoQuery);
		return userInfoQueryList;
	}

	@Override
	public void register(UserLoginInforDTO userLoginInforDTO) {
		//检验用户是否存在
		// 检查学号是否已存在
		if (userLoginInfoMapper.existsByStudentId(userLoginInforDTO.getStudentId())) {
			throw new BusinessException("该学号已被注册");
		}
		UserLoginInfor userLoginInfor = new UserLoginInfor();
		userLoginInfor.setStudentId(userLoginInforDTO.getStudentId());
		userLoginInfor.setNickName(userLoginInforDTO.getNickName());
		userLoginInfor.setStatus(1);
		userLoginInfor.setJoinTime(LocalDateTime.now());
		userLoginInfor.setLastLoginTime(LocalDateTime.now());
		userLoginInfoMapper.insertRegister(userLoginInfor);
	}
}
